<template>
    <div>
        <div style="margin: 10px 0">
            <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-user" v-model="bookName"></el-input>
            <el-input style="width: 200px" placeholder="请输入作者" suffix-icon="el-icon-message" class="mr-l-5" v-model="author"></el-input>
            <el-button class="mr-l-5" type="primary" @click="load">查询</el-button>
            <el-button class="mr-l-5" type="warning" @click="reset">重置</el-button>
        </div>
        <div style="margin: 10px 0">
            <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
            <el-button type="primary"  @click="exp" class="mr-l-5">导出商品名单 <i class="el-icon-top"></i></el-button>
        </div>
        <el-table :data="tableData"  stripe border header-cell-class-name="headerBg"
                  @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column prop="id" label="ID" ></el-table-column>
          <el-table-column prop="name" label="书名" ></el-table-column>
          <el-table-column prop="author" label="作者" ></el-table-column>
            <el-table-column prop="bookSortName" label="类型" ></el-table-column>
          <el-table-column label="图片" >
            <template slot-scope="scope">
              <el-image v-if="scope.row.filesList != null"
                  style="width: 50px; height: 50px"
                  :src= getUrl(scope.row)
                  :preview-src-list= getList(scope.row)
                 ></el-image>
              <el-image v-else>
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline"></i>
                </div>
              </el-image>

            </template>
          </el-table-column>
            <el-table-column prop="price" label="价格" ></el-table-column>
            <el-table-column prop="color" label="颜色" ></el-table-column>
            <el-table-column prop="suit" label="套餐" ></el-table-column>
            <el-table-column prop="count" label="数量" ></el-table-column>
            <el-table-column prop="status" label="状态" width="100px">
              <template slot-scope="scope">
                <el-switch v-model="scope.row.status" @change="changStatus(scope.row)" active-color="#ff4949"
                           inactive-color="#13ce66"></el-switch>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="100px">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="success"
                            @click="handleEdit(scope.row)">编辑<i class="el-icon-edit"></i></el-button>
                </template>
            </el-table-column>
        </el-table>
        <div  class="block" style="padding: 10px 0">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[5, 10, 20, 50]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="totalNum">
            </el-pagination>
        </div>
        <el-dialog title="上架书籍" :visible.sync="dialogFormVisible" width="30%" >
            <el-form label-width="80px" size="small">
                <el-form-item label="书名" ><el-input v-model="form.name" autocomplete="off" placeholder="请输入密码" style="width: 80%"></el-input></el-form-item>
                <el-form-item label="作者" ><el-input v-model="form.author" autocomplete="off" placeholder="请输入密码" style="width: 80%"></el-input></el-form-item>

                <el-form-item label="分类" >
                    <el-cascader
                        v-model="value"
                    :options="options"
                    :props="{ value: 'id', label: 'name',checkStrictly: true }" >
                    </el-cascader>
                </el-form-item>
                <el-form-item label="展示图片" >
                    <el-upload
                        class="upload-demo"
                        action="http://localhost:8000/bookmall/file/upload"
                        :on-success="uploadSuccess"
                        :on-preview="handlePreview"
                        :on-remove="handleRemove"
                        :file-list="fileList"
                        list-type="picture">
                      <el-button size="small" type="primary">点击上传</el-button>
                      <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>
                <el-form-item label="描述" ><el-input v-model="form.dscp" autocomplete="off" placeholder="请输入密码" style="width: 80%"></el-input></el-form-item>
                <el-form-item label="价格" ><el-input v-model="form.price" autocomplete="off" placeholder="请输入密码" style="width: 80%"></el-input></el-form-item>
                <el-form-item label="版本" ><el-input v-model="form.version" autocomplete="off" placeholder="请输入密码" style="width: 80%"></el-input></el-form-item>
                <el-form-item label="颜色" ><el-input v-model="form.color" autocomplete="off" placeholder="请输入密码" style="width: 80%"></el-input></el-form-item>
                <el-form-item label="数量" ><el-input v-model="form.count" autocomplete="off" placeholder="请输入密码" style="width: 80%"></el-input></el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    export default {
        name: "User",
        data(){
            return {
                flag: false,
                tableData: [],
                bookName:'',
                author:'',
                form: {name:''},
                dialogFormVisible: false,
                pageNum: 1,
                pageSize: 10,
                totalNum: 10,
                multipleSelection: [],
                value: 0,
                options:[],
                fileList:[]
            }
        },
        created() {
            //请求分页查询数据
            this.load()
        },
        methods:{
            load(){
                this.request.get('/usedBook/page2',{
                    params: {
                        pageNum: this.pageNum,
                        pageSize : this.pageSize,
                        name: this.bookName,
                        author : this.author
                    }
                }).then(res => {
                    this.tableData = res.data
                   this.totalNum = res.total
                })
            },
            save(){
              if (this.form.id === undefined){
                this.form.bookSortId = this.value.slice(-1)[0]
                const resultList = []
                this.fileList.forEach(file => {
                  file = file.response.data
                  resultList.push(file)
                })
                this.form.filesList = resultList
              }else{
                var files = this.fileList;
                var resultList = []
                files.forEach(file =>{
                  if (file.response != null){
                      resultList.push(file.response.data)
                  }else {
                    resultList.push(file)
                  }
                })
                this.form.filesList = resultList
              }
              if (this.value[1] !== undefined){
                this.form.bookSortId = this.value[1]
              }
               this.request.post("/usedBook/insert", this.form).then(res => {
                    if (res.status === '200'){
                      this.dialogFormVisible= false
                        this.$message.success("操作成功")
                        this.load()
                    }else{
                        this.$message.error("操作失败")
                        this.dialogFormVisible= false;
                    }
               })
            },
            handleDelete(id){
                this.request.delete("/user/"+id).then(res => {
                    if (res){
                        this.$message.success("删除成功")
                        this.pageNum = 1
                        this.load();
                    }else{
                        this.$message.error("删除失败")
                    }
                })
            },

            handleEdit(row){
              this.getSort();
                this.form = row
                this.value = row.bookSortId
              if (row.filesList !== null){
                this.fileList = row.filesList
              }else{
                this.fileList = []
              }

                this.dialogFormVisible= true
            },
            getSort(){
              this.request.get('/bookSort/getAll').then(res => {
                res.data.forEach(item =>{
                  item.children.forEach(child =>{
                    if (child.children.length === 0){
                      child.children = undefined
                    }
                  })
                })
                this.options = res.data
              })
            },
            handleAdd(){
                this.getSort()
                this.form = {}
                this.dialogFormVisible = true
                this.fileList = []
                this.value = 0
            },
            reset(){
                   this.bookName = "",
                    this.author="",
                    this.load();
            },

            handleSelectionChange(val) {
                this.multipleSelection = val
            },
          changStatus(row){
            this.request.post('/usedBook',row).then(res => {
              if (res.status === '200'){
                this.$message.success("成功")
              }else{
                this.$message.error("失败，未知的错误")
              }
            })
          },
            handleSizeChange(pageSize){
                this.pageSize = pageSize
                this.load();
            },
            handleCurrentChange(pageNum){
                this.pageNum = pageNum
                this.load()
            },
            exp(){
                window.open("http://localhost:8000/bookmall/book/export")
            },
            handleSuccess(){
                this.$message.success("导入成功")
                this.load();
            },
            uploadSuccess(response, file, fileList){
              this.fileList = fileList;
            },
            handleRemove(file, fileList) {
              this.fileList = fileList
              const id = file.id
              this.request.delete('/file/'+id).then(res =>{
                  this.$message.success("删除成功")
              })
            },
            handlePreview(file) {
            },
            getUrl(row){
              var result
              var url
              if (row.files !== null){
                  result = JSON.stringify(row.files).split('"')
                url = result[1]
              }
              return url
            },
            getList(row){
              var result
              var i = 1
              var len
              var url =[]
              if (row.files !== null){
                result = JSON.stringify(row.files).split('"')
                result.forEach(res =>{
                    if (res !=="[" && res !=="]" && res !==","){
                      url.push(res)
                    }
                })
              }
              return url
            }

        }
    }
</script>

<style>
.headerBg{
    background: #eee !important;
}
.el-switch__label * {
  line-height: 1;
  font-size: 12px;
  display: inline-block;
}
.el-switch__label {
  position: absolute;
  display: none;
  color: #fff !important;
  font-size: 12px !important;
}
/*打开时文字位置设置*/
.el-switch__label--right {
  z-index: 1;
  right: 22px;
}
/*关闭时文字位置设置*/
.el-switch__label--left {
  z-index: 1;
  left: 20px;
}
/*显示文字*/
.el-switch__label.is-active {
  display: block;
}
/*开关宽度*/
.el-switch .el-switch__core,
.el-switch .el-switch__label {
  width: 45px !important;
}

</style>